<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>活动列表</title>
  <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/bootstrap/4.5.2/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
  <style>
    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      background-color: #f8f9fa;
      color: #343a40;
    }
    .activity-list {
      padding: 0 30px;
      max-width: 1200px;
      margin: 0 auto;
    }
    #activities {
      height: calc(100vh - 160px);
      overflow-y: auto;
      padding-right: 10px;
    }
    .activity-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      background-color: #ffffff;
      padding: 20px 24px;
      border-radius: 10px;
      margin-bottom: 15px;
      box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
      transition: all 0.3s ease;
      border-left: 4px solid #4e73df;
    }
    .activity-item:hover {
      transform: translateY(-2px);
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    }
    .activity-info {
      width: calc(100% - 150px);
    }
    .activity-info h4 {
      width: 100%;
      font-weight: 700;
      font-size: 16px;
      color: #2e3a59;
      margin-bottom: 8px;
    }
    .activity-info p {
      font-weight: 400;
      font-size: 14px;
      color: #6c757d;
      margin: 0;
      line-height: 24px;
    }
    .active a {
      display: inline-flex;
      align-items: center;
      font-weight: 500;
      font-size: 15px;
      color: #fff;
      background-color: #4e73df;
      padding: 8px 16px;
      border-radius: 20px;
      text-decoration: none;
      transition: all 0.3s;
    }
    .active a:hover {
      background-color: #3a56c4;
      transform: translateY(-2px);
      box-shadow: 0 4px 8px rgba(78, 115, 223, 0.3);
    }
    .active a i {
      margin-right: 6px;
      font-size: 14px;
    }
    .inactive a {
      display: inline-flex;
      align-items: center;
      font-weight: 500;
      font-size: 15px;
      color: #858796;
      background-color: #eaecf4;
      padding: 8px 16px;
      border-radius: 20px;
      text-decoration: none;
    }
    .inactive a i {
      margin-right: 6px;
      font-size: 14px;
    }
    .pagination {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      margin-top: 25px;
      padding-right: 16px;
    }
    .pagination button {
      margin: 0 5px;
      border-radius: 6px;
      border: 1px solid #d1d3e2;
      background-color: #fff;
      padding: 6px 12px;
      cursor: pointer;
      font-size: 14px;
      color: #6e707e;
      transition: all 0.2s;
    }
    .pagination button:hover {
      background-color: #eaecf4;
      border-color: #bac8f3;
    }
    .pagination span {
      margin: 0 12px;
      font-size: 14px;
      color: #6e707e;
    }
    .pagination input {
      width: 60px;
      text-align: center;
      border: 1px solid #d1d3e2;
      border-radius: 4px;
      padding: 4px;
      margin: 0 5px;
    }
    .activity-list h2 {
      font-weight: 700;
      font-size: 24px;
      color: #2e3a59;
      height: 80px;
      display: flex;
      align-items: center;
      margin-bottom: 0;
      border-bottom: 1px solid #e3e6f0;
      padding-bottom: 15px;
    }
    /* 自定义滚动条 */
    #activities::-webkit-scrollbar {
      width: 8px;
    }
    #activities::-webkit-scrollbar-track {
      background: #f1f1f1;
      border-radius: 10px;
    }
    #activities::-webkit-scrollbar-thumb {
      background: #c1c5d1;
      border-radius: 10px;
    }
    #activities::-webkit-scrollbar-thumb:hover {
      background: #a7aab8;
    }
    .empty-state {
      text-align: center;
      padding: 40px 0;
      color: #858796;
    }
    .empty-state i {
      font-size: 60px;
      margin-bottom: 15px;
      color: #dddfeb;
    }
  </style>
</head>
<body>
<div class="activity-list">
  <h2><i class="fas fa-list-alt mr-2"></i> 活动列表</h2>
  <div id="activities">
    <!-- 活动列表将动态插入这里 -->
  </div>
  <div class="pagination">
    <button class="btn-outline-primary" onclick="fetchActivities(1)"><i class="fas fa-angle-double-left"></i> 首页</button>
    <button class="btn-outline-primary" onclick="previousPage()"><i class="fas fa-angle-left"></i> 上一页</button>
    <span>第 <input type="number" id="pageInput" min="1" value="1" /> 页 / 共 <span id="totalPages">1</span> 页</span>
    <button class="btn-outline-primary" onclick="nextPage()">下一页 <i class="fas fa-angle-right"></i></button>
    <button class="btn-outline-primary" onclick="fetchActivities(totalPages)">尾页 <i class="fas fa-angle-double-right"></i></button>
  </div>
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
  var currentPage = 1;
  var pageSize = 10;
  var totalPages;
  var userToken = localStorage.getItem("user_token");

  // 发送AJAX请求的函数
  function fetchActivities(page) {
    // 如果页码小于1，则重置为1
    if (page < 1) {
      page = 1;
    }
    // 更新当前页码
    currentPage = page;
    // 构建要发送的数据对象
    var dataToSend = {
      currentPage: currentPage,
      pageSize: pageSize
    };
    // 发送AJAX请求
    $.ajax({
      url: '/activity/find-list',
      type: 'GET',
      data: dataToSend, // 将分页参数作为请求数据发送
      dataType: 'json',
      headers: {
        // jwt
        "user_token": userToken
      },
      success: function(result) {
        if (result.code != 200) {
          alert("查询活动列表失败！" + result.msg);
        } else {
          var activities = result.data.records; // 假设返回的数据中活动列表字段为 'records'
          var activitiesHtml = '';
          var listContainer = document.getElementById('activities');
          
          // 在添加新内容前，先清空listContainer
          listContainer.innerHTML = '';
          
          if (activities.length === 0) {
            activitiesHtml = `
              <div class="empty-state">
                <i class="fas fa-calendar-times"></i>
                <p>暂无活动数据</p>
              </div>
            `;
          } else {
            activities.forEach(function(activity) {
              var url = 'draw.html?activityName='+ encodeURIComponent(activity.activityName)
                      +'&activityId=' + encodeURIComponent(activity.activityId)
                      +'&valid=' + encodeURIComponent(activity.valid);
              var linkTextActive = `<a href="${url}" target="_blank"><i class="fas fa-ticket-alt"></i>活动进行中，去抽奖</a>`;
              var linkTextInactive = `<a href="${url}" target="_blank"><i class="fas fa-clipboard-list"></i>活动已完成，查看中奖名单</a>`;
              var validClass = activity.valid ? 'active' : 'inactive';
              var link = activity.valid ? linkTextActive : linkTextInactive;
              activitiesHtml += `
                        <div class="activity-item">
                          <div class="activity-info">
                            <h4>${activity.activityName}</h4>
                            <p>${activity.description}</p>
                          </div>
                          <div class="${validClass}">
                            <p>${link}</p>
                          </div>
                        </div>
                      `;
            });
          }
          
          $('#activities').html(activitiesHtml);
          // 更新分页控件的总页数
          totalPages = Math.ceil(result.data.total / pageSize);
          // 更新输入框的值
          $('#pageInput').val(currentPage);
          $('#totalPages').text(totalPages);
        } // else end
      },
      error:function(err){
        console.log(err);
        if(err!=null && err.status==401){
          alert("用户未登录, 即将跳转到登录页!");
          // 跳转登录页
          window.location.href = "/blogin2.html";
          window.parent.location.href = "/blogin2.html";//让父页面一起跳转
        }
      }
    });
  }

  function previousPage() {
    if (currentPage > 1) {
      fetchActivities(currentPage - 1);
    } else {
      alert("已经是第一页");
    }
  }

  function nextPage() {
    if (currentPage < totalPages) {
      fetchActivities(currentPage + 1);
    } else {
      alert("已经是最后一页");
    }
  }

  $(document).ready(function() {
    fetchActivities(1);
  });

  // 绑定输入框回车事件
  $('#pageInput').on('keypress', function(e) {
    if (e.key === 'Enter') {
      var page = parseInt(this.value);
      if(page > totalPages){
        page = totalPages
        $('#pageInput').val(totalPages);
      }
      if (!isNaN(page) && page >= 1 && page <= totalPages) {
        fetchActivities(page);
      }
    }
  });
</script>
</body>
</html>